package com.pz.gulimall.member.mq;

import com.pz.gulimall.common.exception.BizCodeEnum;
import com.pz.gulimall.common.exception.RRException;
import com.pz.gulimall.common.to.mq.OrderPayFinishedMsg;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class MemberListener {
    /**
     * 处理支付成功订单后的用户数据
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "order.finnish.user.que"),
            exchange = @Exchange(name = "orderEvent.exchange", type = ExchangeTypes.TOPIC),
            key = {"order.finish.#"}
    ))
    @RabbitHandler
    public void handleOrderFinnishUser(OrderPayFinishedMsg finishedMsg, Message msg, Channel channel) {
        long deliveryTag = msg.getMessageProperties().getDeliveryTag();
        log.info("处理用户-{}", finishedMsg);
        // TODO 用户积分等级更新
        try {
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            throw new RRException(BizCodeEnum.RABBITMQ_ACK_EXCEPTION, e.getMessage());
        }
    }

}
